#pragma once
#include <assert.h>
namespace zkf
{
	template<class T>
	struct list_node
	{
	
		list_node(const T& s = T())
			:_date(s)
			,_next(nullptr)
			,_prev(nullptr)
		{
		}
		T _date;
		list_node<T>* _next;
		list_node<T>* _prev;
	};
	template<class T,class ref,class def>
	struct list_iterator
	{
		typedef list_node<T> node;
		node* _c;

		typedef list_iterator<T,ref,def> self;
		
		list_iterator(node* node)
			: _c(node)
		{
		}
		self& operator++()
		{
			_c = _c->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp (*this);
			_c = _c->_next;
			return tmp;
		}
		self& operator--()
		{
			_c = _c->_prev;
			return *this;
		}
		self operator--(int)
		{
			self tmp(*this);
			_c = _c->_prev;
			return tmp;
		}
		ref operator*()
		{
			return _c->_date;
		}
		bool operator!=(const self& s)const
		{
			return _c != s._c;
		}
		bool operator==(const self& s) const
		{
			return _c == s._node;
		}
		def operator->()
		{
			return &_c->date;
		}
	};
	template<class T>
	class list
	{
		typedef list_node<T> node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		void empty_init()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list()
		{
			empty_init();
		}
		list(const list<T>& s)
		{
			empty_init();
			for (auto& e : s)
			{
				push_back(e);
			}
		}
		list(initializer_list<T> il)
		{
			empty_init();
			for (auto& e : il)
			{
				push_back(e);
			}
		}
		void push_back(const T& s)
		{
			node* newnode = new node(s);
			node* tmp = _head->_prev;
			tmp->_next = newnode;
			newnode->_prev = tmp;
			newnode->_next = _head;
			_head->_prev = newnode;
			_size++;
		}
		void pop_back()
		{
			node* tmp = _head->_prev;
			_head->_prev = tmp->_prev;
			tmp->_prev->_next = _head;
			delete[] tmp;
			_size--;
		}
		iterator insert(iterator pos, const T& s)
		{
			node* cur = pos._c;
			node* prev = cur->_prev;
			node* newnode = new node(s);
			newnode->_next = cur;
			newnode->_prev = prev;
			prev->_next = newnode;
			cur->_prev = newnode;
			_size++;
			return newnode;
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			node* prev = pos._c->_prev;
			node* next = pos._c->_next;
			prev->_next = next;
			next->_prev = prev;
			delete pos._c;
			--_size;
			return next;
		}
		void push_front(const T& s)
		{
			insert(begin(), s);
		}
		void pop_front()
		{
			erase(begin());
		}
		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin()const
		{
			return _head->_next;
		}
		const_iterator end()const
		{
			return _head;
		}
		size_t size()const
		{
			return _size;
		}
		bool empty()const
		{
			return _size == 0;
		}
		void clear()
		{
			auto it = begin();
			while(it!=end())
			{
				it=erase(it);
			}
		}
		void swap(list<T>& s)
		{
			std::swap(_head,s._head);
			std::swap(_size, s._size);
		}
		list<T>& operator=(list<T> s)
		{
			swap(s);
			return *this;
		}
		~list()
		{
			clear();
			delete[] _head;
			_head = nullptr;
			_size = 0;
		}
	private:
		node* _head;
		size_t _size;

	};
}